﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.DataEntity;
using TimKen.DataAccess;
using System.Web.Services;
using System.Data;
using Common;
using TimKen.BussLogic;
namespace TimKen.Web.Shop
{
    public partial class ShopEdit : TimKen.App.WebPage
    {
        public string JsonHotel;
        public ShopCartSet Shop {
            get { return Session["ShopCartSet"] as ShopCartSet; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (CurrentUserName != null && CurrentUserName != "")
                {
                    if (Request["type"] != null && Request["type"].ToString() != "")
                    {
                        ViewState["type"] = Request["type"].ToString();
                        ViewState["id"] = Request["type"].ToString() == "update" ? Request["ProJectId"].ToString() : "";
                        JsonHotel = ViewState["hJson"] != null ? ViewState["hJson"].ToString() : "";
                        if (Request["type"].ToString() == "update" && Request["ProJectId"] != null && Request["ProJectId"].ToString() != "")
                        {

                            ShopCartItem item = Shop[Request["ProJectId"].ToString().Trim()];
                            this.ProCode.Value = item.Project.ProjectId;
                            this.ProLx.Value = item.Project.ProjectXh;
                            this.Num.Value = item.Count.ToString();
                            this.Dprice.Value = item.Project.Unitprice.ToString();
                            this.Money.Value = item.Money.ToString();
                            this.NewBs.Value = item.Project.Min.ToString();
                            this.jz.Value = item.Project.Netweight.ToString();
                            this.MinDl.Value = item.Project.MinQuantitative.ToString();
                            this.Date.Value = item.Project.Date.ToString("yyyy-MM-dd");
                            this.Hd_sl.Value = item.Project.TaxRate.ToString().Trim();
                   
                        }
                    }
                    else
                    {
                        Response.Redirect("ShopProJect.aspx");
                    }
                }
                else {

                    Alert("您必须登录！", "../Index.aspx");
                }
            }
        }
        [WebMethod]
        public static string getJsonHotel(string id)
        {
            ProductDAO dao = new ProductDAO();
            DataTable dt = dao.getProList(id.Trim());
            if (dt != null && dt.Rows.Count > 0)
            {
                return JsonHelper.DataTableToJson(dt);
            }
            else {
                return "";
            }
        }
        ProductManager pm = new ProductManager();
        UserAccountManager uam = new UserAccountManager();
        protected void Img_But_Click(object sender, ImageClickEventArgs e)
        {
            if(ViewState["id"]!=null&&ViewState["id"].ToString()!="")
            {
                Shop.Remove(ViewState["id"].ToString().Trim());
            }
            Project pro = new Project();
            pro.Date = Convert.ToDateTime( this.Date.Value.Trim());
            pro.Min = Convert.ToInt32(this.NewBs.Value.Trim());
            pro.MinQuantitative = Convert.ToInt32(this.MinDl.Value.Trim());
            pro.Netweight = float.Parse(this.jz.Value.Trim());
            pro.ProjectId = this.ProCode.Value.Trim();
            pro.ProjectXh = this.ProLx.Value.Trim();
            pro.Unitprice = Convert.ToDecimal(this.Dprice.Value.Trim());
            pro.TaxRate =float.Parse(this.Hd_sl.Value);
            bool isAuthorizationed = pm.GetProductAuthorization(Convert.ToInt32(pro.ProjectId), uam.GetModel(CurrentUserName).CustomerID.Value);
            ShopCartItem item = new ShopCartItem(pro, Convert.ToInt32(this.Num.Value.Trim()), isAuthorizationed);
            Shop.Add(item);
            ClientScript.RegisterStartupScript(this.GetType(), "tanyo", "<script>CloseBox();</script>", false);
           // Response.Redirect("ShopProJect.aspx");
        }

      
    }
    
}